<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Spinatiev Ivan
 * Date: 11/6/12
 * Time: 12:05 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_Amounts extends Page
{
	/**
	 * @var Db_Products_Amounts
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->actionsTableObject = array(
			'addEditAction',
			'saveAction',
            'saveDefaultAction',
		);
		$this->classTableObject = 'Db_Products_Amounts';
		parent::__construct();
	}

	public function defaultAction() {
		if (perm('PRODUCTS_AMOUNTS_EDIT')) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		}
		$allAmounts = Db_Products_Amounts::findAllArray();
		self::$TPL->assign('allAmounts', $allAmounts);
		self::$TPL->setBodyTemplate('list_amounts');
		return array(
			'type' => 'display'
		);
	}

    public function saveDefaultAction() {
        perm('PRODUCTS_AMOUNTS_EDIT', '/products/amounts');

        $this->message = t(T_FORM_MSG, 'success');
        if (!empty($this->tableObject->objectArray)) {
			Db_Products_Amounts::removeDefaultAmount();
            $this->tableObject->am_is_default = 1;
            if(!$this->tableObject->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }

        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

	public function addEditAction() {
		perm('PRODUCTS_AMOUNTS_EDIT', '/products/amounts');
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->am_amount . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true);
			self::$TPL->assign('amount', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}
		self::$TPL->setBodyTemplate('add_edit_amounts');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('PRODUCTS_AMOUNTS_EDIT', '/products/amounts');
		$amount = post('am_amount', 'int');
		$this->message = t(T_FORM_MSG, 'success');
		if(!empty($amount)){
			if(!empty($this->tableObject->objectArray->id)) {
				$this->tableObject->id = $this->idTableObject;
			}
			$this->tableObject->am_amount = $amount;
			$this->tableObject->am_is_sample = post('am_is_sample', 'int');
			$this->tableObject->am_is_default = post('am_is_default', 'int');
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('PRODUCTS_AMOUNTS_EDIT', '/products/amounts');
		$this->message = t(T_FORM_MSG, 'success');

		if(!Db_Products_Amounts::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}
